<?php
namespace app\admin\controller;
class Deploy extends Base
{
    /**
     * 配置选项
     * @return mixed
     */
    public function lists()
    {
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['type']!='') $map[] = ['and','type','=',$d['type']];
            if($d['area']!='') $map[] = ['and','area','=',$d['area']];
            if($d['group']!='') $map[] = ['and','group','=',$d['group']];
            if($d['keyword']!='') $map[] = ['and',"(name like ? or title like ? or remark like ?)",["%{$d['keyword']}%","%{$d['keyword']}%","%{$d['keyword']}%"]];
            $w  = where($map);
            $rs = $this->db->query("select * from deploy $w[0] order by sort desc, id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            //com_sql();
            if($rs){
                return suc($rs[0], $rs[1]);
            }else{
                return suc([],0);
            }
        }
        $this->fetch();
    }
    //添加
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule    = [
                'name' => 'require|unique:deploy,name',
                'title' => 'require',
                'area' => 'require',
                'type' => 'require',
                'group' => 'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            cookie('js_msg','添加成功!');
            cookie('js_function','preload');
            $this->db->insert('deploy',$d);
            return suc();
        }
        return $this->fetch();
    }
    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule    = [
                'name' => "require|unique:deploy,name,{$d['id']},id",
                'title' => 'require',
                'area' => 'require',
                'type' => 'require',
                'group' => 'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            cookie('js_msg','编辑成功!');
            cookie('js_function','preload');
            $this->db->update('deploy',$d);
            return suc();
        }
        $id = $this->get['id'];
        $info = $this->db->find('deploy',$id);
        return $this->fetch(['info'=>$info]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $rs = m('Deploy','xcom')->del($d['id']);
            cookie('js_function','reload');
            if($rs){
                return suc();
            }else{
                return err(model('Deploy','xcom')->getError());
            }
        }
    }
    //详情
    public function detail(){
        return $this->fetch();
    }
    //配置选项
    public function group()
    {
        if(is_post()){
            $d = $this->post;
            $rs = m('Deploy','xcom')->batchSave($d);
            if($rs){
                cookie('js_msg','保存成功!');
                return suc();
            }else{
                return err(m('Deploy','xcom')->getError());
            }
        }
        $id = $this->get('id');
        $id = $id ? $id : 1;
        $data = $this->db->select('deploy',['`group`=?',[$id]]);
        return $this->fetch(['list' => $data,'group_id' => $id]);
    }
}
